﻿#!/usr/bin/env python
#coding:utf-8
from probe.core.level import *
import time
from commands import *
def online(args):
    '''根据端口获取连接数'''
    result = getoutput('netstat -ant|grep -E "%s"|wc -l'%args.get('Ports'))
    return result

def get_online(args):
    '''监控在线人数'''
    result = online(args)
    if int(result) >= args.get('WarningData') or int(result) <= args.get('Lessnum'):
        return WarningData({'在线人数': [int(result), '人']})
    return InfoData({'在线人数': [int(result), '人']})

def compare_online(args):
    '''监控每分钟人数落差,时间不同,伐值不同'''
    nowtime = int(time.strftime("%H%M",time.localtime()))
    if nowtime > 300 and nowtime < 400 or nowtime > 2000 and nowtime < 2200:
        Warning = 200
    elif nowtime >= 400 and nowtime <= 2000:
        Warning = 100
    else:
        Warning = 400
    result1 = online(args)
    time.sleep(int(args.get('Timeinterval')))
    result2 = online(args)
    result  = int(result2) - int(result1)
    if int(result) >= Warning:
        return WarningData({'人数落差': [int(result), '人']})
    return InfoData({'人数落差': [int(result), '人']})
        
if __name__ == '__main__':
    print get_online({'Lessnum': 100, 'WarningData': 2000, 'Ports': '2888[0-6]'})
    print compare_online({'Ports': '2888[0-6]', 'Timeinterval': 60})
